<?php

namespace Home\Controller;

use Admin\Controller\CommonController;
use Think\Controller;

class LoginController extends CommoController
{

    //  验证用户名是否存在

    public function checkAddUser()
    {
        $username = $_POST['username'];
        $model_check_user = D('user');
        $sql = sprintf("SELECT * FROM user WHERE username = '%s'", $username);
        $res = $model_check_user->query($sql);
        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '可用' : '不可用';
        $this->ajaxRet($code, $msg);

    }


    //  注册

    public function addLogin()
    {
        $username = $_POST['username'];
        $model_login = D('user');
        $sql = sprintf("SELECT * FROM user WHERE username ='%s'", $username);
        $res = $model_login->query($sql);
        if (!$res) {
        } else {
            $this->ajaxRet(self::RET_SUCCESS, '用户名已经存在');
        }

        if (IS_POST) {
            $data['username'] = I('post.username');
            $data['password'] = I('post.password');
            if ($model_login->add($data)) {
                $this->ajaxRet(self::RET_FAILED, '注册成功');
            }
            $this->ajaxRet(self::RET_SUCCESS, '注册失败');
        }
    }


    // 验证登录

    public function checkLogin()
    {
        $data['username'] = $_POST['username'];
        $data['password'] = $_POST['password'];
        $model_check_login = D('user');
        $rule = where_model()->parseWhere($data);
        $res = $model_check_login->query("SELECT * FROM user WHERE $rule");
        session('member', $res);
        $code = !$res ? self::RET_FAILED : self::RET_SUCCESS;
        $msg = !$res ? '登录失败' : '登录成功';
        $this->ajaxRet($code, $msg);

    }
}